package com.heima.datasync.EsDataService.impl;

import com.heima.datasync.mapper.ApArticleMapper;
import com.heima.datasync.EsDataService.EsDataService;
import com.heima.es.service.EsService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.vos.SearchArticleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EsDataServiceImpl implements EsDataService {

    public static final String ARTICLE_INDEX_NAME = "app_info_article";

    @Autowired
    ApArticleMapper apArticleMapper;
    @Autowired
    private EsService<SearchArticleVO> esService;
    @Override
    public ResponseResult dataInit() {
        //判断索引库是否存在
        boolean result = esService.existIndex(ARTICLE_INDEX_NAME);
        if (result){
            //存在则删除
            esService.deleteIndex(ARTICLE_INDEX_NAME);
        }
        //创建索引库
        esService.createIndex(
                "{\n" +
                "    \"mappings\":{\n" +
                "        \"properties\":{\n" +
                "            \"id\":{\n" +
                "                \"type\":\"long\"\n" +
                "            },\n" +
                "            \"publishTime\":{\n" +
                "                \"type\":\"date\"\n" +
                "            },\n" +
                "            \"layout\":{\n" +
                "                \"type\":\"integer\"\n" +
                "            },\n" +
                "            \"images\":{\n" +
                "                \"type\":\"keyword\",\n" +
                "                \"index\": false\n" +
                "            },\n" +
                "           \"staticUrl\":{\n" +
                "                \"type\":\"keyword\",\n" +
                "                \"index\": false\n" +
                "            },\n" +
                "            \"authorId\": {\n" +
                "          \t\t\"type\": \"long\"\n" +
                "       \t\t},\n" +
                "          \"title\":{\n" +
                "            \"type\":\"text\",\n" +
                "            \"analyzer\":\"ik_smart\"\n" +
                "          }\n" +
                "        }\n" +
                "    }\n" +
                "}", ARTICLE_INDEX_NAME);
        //查询文章
        List<ApArticle> allArticles = apArticleMapper.findAllArticles();
        //判断是否有数据
        if (allArticles == null || allArticles.size() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "文查询到数据库文章数据");
        }
        //遍历数据集合封装
        List<SearchArticleVO> list = new ArrayList<>();
        for (ApArticle allArticle : allArticles) {
            SearchArticleVO searchArticleVO = new SearchArticleVO();
            BeanUtils.copyProperties(allArticle, searchArticleVO);
            list.add(searchArticleVO);
        }
        esService.saveBatch(list, ARTICLE_INDEX_NAME);
        return ResponseResult.okResult();
    }
}
